package com.treasure.mall.zbqmallmanager.controller.wx;

import com.treasure.mall.biz.dto.condition.BaseSearchCondition;
import com.treasure.mall.biz.dto.wechat.KeywordMessageDTO;
import com.treasure.mall.biz.entity.wechat.WechatKeywordMessagePO;
import com.treasure.mall.biz.service.core.wechat.WechatKeywordMessageService;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 *
 * <p>
 * +---------------------------------------------------------
 * * @package: com.treasure.mall.zbqmallmanager.controller.wx
 * * @author: guomw (guomwchen@foxmail.com)
 * * @date: 2020/9/15 11:38 上午
 * * @since: V1.0.0
 * +---------------------------------------------------------
 * 注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的
 *
 */
@Controller
@RequestMapping("/wx")
public class WxController {

    @Autowired
    private WechatKeywordMessageService keywordMessageService;

    /**
     * 微信关键字回复列表
     *
     * @param condition
     * @param pageIndex
     * @param model
     * @return
     */
    @GetMapping("/keyword/list")
    @SuppressWarnings("Duplicates")
    public String normalList(
            @ModelAttribute("condition") BaseSearchCondition condition,
            @RequestParam(required = false, defaultValue = "1") int pageIndex,
            Model model
    ) {
        Page<WechatKeywordMessagePO> pos = keywordMessageService.findAll(pageIndex, Constant.DEFAULT_PAGE_SIZE, condition);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("totalRecords", pos.getTotalElements());
        model.addAttribute("totalPages", pos.getTotalPages());
        model.addAttribute("list", pos.getContent());
        return "wx/keyword_list";
    }

    @PostMapping("/keyword/list/save")
    @ResponseBody
    public ApiResult save(KeywordMessageDTO dto) {
        keywordMessageService.save(dto);
        return ApiResult.ok();
    }

    /**
     * 删除关键字回复
     *
     * @param id
     * @return
     */
    @GetMapping("/keyword/list/delete")
    @ResponseBody
    public ApiResult deleteById(@RequestParam Long id) {
        keywordMessageService.delete(id);
        return ApiResult.ok();
    }
}
